﻿using UnityEngine;
using UnityEngine.UI;
using ZCUIManager;

namespace ZCTouchDrawBoard
{
    /// <summary>
    /// 图纸面板
    /// </summary>
    public class PaperPanel : UIBaseView
    {
        #region Private UI
        [SerializeField] private float m_ScreenRatio = 0.6f;
        private InputField m_InputWidth;
        private InputField m_InputHeight;
        private Button m_ButtonCreate;
        private Button m_ButtonEdit;
        private Button m_ButtonAdd;
        private Button m_ButtonCancel;
        #endregion

        #region PrivateField
        /// <summary>
        /// 选中的图纸
        /// </summary>
        private BasePaperView m_SelectPaperView;
        #endregion

        #region PublicMethod
        /// <summary>
        /// 主View
        /// </summary>
        public TouchDrawingView TouchDrawingView { get; set; }

        /// <summary>
        /// 选中的图纸
        /// </summary>
        public BasePaperView SelectPaperView => m_SelectPaperView;

        /// <summary>
        /// 创建图纸的委托
        /// </summary>
        public CreatePaperCallback CreatePaperCallback { get; set; }

        /// <summary>
        /// 设置图纸的点击委托
        /// </summary>
        public ClickPaperCallback ClickPaperCallback { get; set; }

        /// <summary>
        /// 设置图纸的点击按下委托
        /// </summary>
        public ClickDownPaperCallback ClickDownPaperCallback { get; set; }

        /// <summary>
        /// 设置图纸的点击弹起委托
        /// </summary>
        public ClickUpPaperCallback ClickUpPaperCallback { get; set; }

        /// <summary>
        /// 设置图纸的拖拽委托
        /// </summary>
        public DragPaperCallback DragPaperCallback { get; set; }

        /// <summary>
        /// 图纸编辑状态改变委托
        /// </summary>
        public EditPaperCallback EditPaperCallback { get; set; }
        #endregion

        #region UI
        public override void Initialize(eUIType uiType, GameObject go)
        {
            base.Initialize(uiType, go);
            m_InputWidth = Find(m_RectTransform, "InputWidth").GetComponent<InputField>();
            m_InputWidth.text = Mathf.RoundToInt(Screen.width * m_ScreenRatio).ToString();
            m_InputHeight = Find(m_RectTransform, "InputHeight").GetComponent<InputField>();
            m_InputHeight.text = Mathf.RoundToInt(Screen.height * m_ScreenRatio).ToString();
            m_ButtonCreate = Find(m_RectTransform, "ButtonCreate").GetComponent<Button>();
            m_ButtonEdit = Find(m_RectTransform, "ButtonEdit").GetComponent<Button>();
            m_ButtonAdd = Find(m_RectTransform, "ButtonAdd").GetComponent<Button>();
            m_ButtonCancel = Find(m_RectTransform, "ButtonCancel").GetComponent<Button>();
        }

        public override void InitializeListener()
        {
            base.InitializeListener();
            m_ButtonCreate.onClick.AddListener(OnClickCreateListener);
            m_ButtonEdit.onClick.AddListener(OnClickEditListener);
            m_ButtonAdd.onClick.AddListener(OnClickAddListener);
            m_ButtonCancel.onClick.AddListener(OnClickCancelListener);
        }

        public override void Show()
        {
            base.Show();
            UpdateButtons();
        }
        #endregion

        #region Listener
        private void OnClickCreateListener()
        {
            int width = int.Parse(m_InputWidth.text);
            int height = int.Parse(m_InputHeight.text);
            CreatePaper(ePaperType.ALPHA, width, height);
        }

        private void OnClickEditListener()
        {
            EditPaper(m_SelectPaperView.gameObject, true);
            UpdateButtons();
        }

        private void OnClickAddListener()
        {
            OnClickCreateListener();
        }

        private void OnClickCancelListener()
        {
            EditPaper(m_SelectPaperView.gameObject, false);
            UpdateButtons();
        }

        private void UpdateButtons()
        {
            m_ButtonCreate.gameObject.SetActive(false);
            m_ButtonEdit.gameObject.SetActive(false);
            m_ButtonCancel.gameObject.SetActive(false);

            if (m_SelectPaperView == null)
            {
                m_ButtonCreate.gameObject.SetActive(true);
            }
            else
            {
                if (m_SelectPaperView.Editing)
                {
                    m_ButtonCancel.gameObject.SetActive(true);
                }
                else
                {
                    m_ButtonEdit.gameObject.SetActive(true);
                }
            }
        }
        #endregion

        #region 功能
        /// <summary>
        /// 创建图纸
        /// </summary>
        /// <param name="paperType">图纸类型</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <param name="depth">深度</param>
        /// <param name="">创建后的回调</param>
        public BasePaperView CreatePaper(ePaperType paperType, int width, int height, int depth = 16)
        {
            BasePaperView paper = null;
            switch (paperType)
            {
                case ePaperType.NONE:
                    break;
                case ePaperType.NORMAL:
                    paper = UIManager.Instance.GetUIView<NormalPaperView>(eUIType.NormalPaperView);
                    break;
                case ePaperType.ALPHA:
                    paper = UIManager.Instance.GetUIView<AlphaPaperView>(eUIType.AlphaPaperView);
                    break;
                default:
                    break;
            }
            if (paper != null)
            {
                paper.Initialize(width, height, depth);
                paper.TouchDrawingView = TouchDrawingView;
                if (CreatePaperCallback != null)
                {
                    CreatePaperCallback?.Invoke(paper.gameObject);
                }
                else
                {
                    SwitchPaper(paper.gameObject);
                }
            }
            return paper;
        }

        /// <summary>
        /// 切换图纸
        /// </summary>
        /// <param name="paperGo"></param>
        public void SwitchPaper(GameObject paperGo)
        {
            m_SelectPaperView = paperGo.GetComponent<BasePaperView>();
            UpdateButtons();
        }

        /// <summary>
        /// 编辑图纸
        /// </summary>
        /// <param name="paperGo"></param>
        /// <param name="editing"></param>
        public void EditPaper(GameObject paperGo, bool editing)
        {
            BasePaperView paper = paperGo.GetComponent<BasePaperView>();
            paper.Editing = editing;
            EditPaperCallback?.Invoke(editing);
        }

        /// <summary>
        /// 删除图纸
        /// </summary>
        /// <param name="paperGo"></param>
        public void DeleteSelectPaper(GameObject paperGo)
        {
            BasePaperView paper = paperGo.GetComponent<BasePaperView>();
            paper.Release();
            GameObject.Destroy(paperGo);
        }
        #endregion
    }
}
